// pages/shop/order.js
var app = getApp();
var orderId = 0;
var currentShopId = 0;
Page({
  data:{
    showRefundReject: false
  },
  onLoad:function(options){
    orderId = options.id;
    var shop = wx.getStorageSync('shop');
    currentShopId = shop.id;
  },
  onReady:function(){
    var that = this;
    wx.showLoading({title: '订单加载中...', mask: true});
    app.func.req('orders/show', {
      id: orderId,
      is_business: true
    }, 'GET', function(res){
      console.log(res.result)
      var order = res.result
      order.status_alias = app.util.orderStatus(order.status)
      that.setData({
        order: order
      });
      wx.hideLoading();
    }, {
      token: wx.getStorageSync('token')
    })
  },
  onShow:function(){
    var that = this;
    wx.showLoading({title: '订单加载中...', mask: true});
    app.func.req('orders/show', {
      id: orderId,
      is_business: true
    }, 'GET', function(res){
      console.log(res.result)
      var order = res.result
      order.status_alias = app.util.orderStatus(order.status)
      that.setData({
        order: order
      });
      wx.hideLoading();
    }, {
      token: wx.getStorageSync('token')
    })
  },
  //确认订单
  notifyOrder: function(){
    var that = this;
    if (this.data.order.is_notify == 1) {
      return false
    }
    wx.showLoading({
      title: '提交中',
      mask: true
    })
    app.func.req('orders/notify', {
      id: orderId
    }, 'POST', function (res) {
      wx.hideLoading()
      console.log(res);
      if (res.code == 'success') {
        wx.showToast({
          title: '通知成功',
          icon: 'success',
          duration: 2000
        })
        var order = that.data.order;
        order.is_notify = 1;
        that.setData({
          order: order
        });
      } else {
        wx.showToast({
          title: '通知失败',
          image: '/images/fail.png',
          duration: 2000
        })
      }
    }, {
      token: wx.getStorageSync('token')
    })

  },
  //取消订单
  cancelOrder: function(){
    this.OrderUpdateStatus('取消订单', 3);
  },
  //发货
  delivery: function(){
    wx.navigateTo({
      url: './send?id=' + orderId
    });
  },
  OrderUpdateStatus: function(title,status){
    app.func.req('orders/status', {
      id: orderId,
      shop_id: currentShopId,
      status: status
    }, 'POST', function (res) {
      console.log(res);
      if (res.code == 'success') {
        wx.showToast({
          title: title,
          icon: 'success',
          duration: 2000
        })
      }
    }, {
        token: wx.getStorageSync('token')
      })
  },
  auditRefund: function(){
    app.func.req('shops/refund_audit', {
      id: orderId,
      shop_id: currentShopId,
      status: 10
    }, 'POST', function (res) {
      console.log(res);
      if (res.code == 'success') {
        wx.showModal({
          title: '提示',
          content: '已提交，等待处理',
          showCancel: false,
          success: function () {
            setTimeout(function () {
              wx.navigateBack()
            }, 1000)
          }
        });
      } else {
        wx.showToast({
          title: '提交失败',
          image: '/images/fail.png',
          duration: 2000
        });
      }
    }, {
        token: wx.getStorageSync('token')
      })
  },
  refundReject: function (e) {
    var that = this;
    var reason = e.detail.value.reason;
    if (reason.length < 5) {
      wx.showModal({
        title: '提示',
        content: '拒绝说明不少于5个字',
        showCancel: false
      });
      return;
    }
    this.setData({
      showRefundReject: false
    })
    app.func.req('shops/refund_audit', {
      id: orderId,
      status: 9,
      reason: reason,
    }, 'POST', function (res) {
      if (res.code == 'success') {
        wx.showModal({
          title: '提示',
          content: '已提交，等待处理',
          showCancel: false,
          success: function () {
            setTimeout(function () {
              wx.navigateBack()
            }, 1000)
          }
        });
      } else {
        wx.showToast({
          title: '提交失败',
          image: '/images/fail.png',
          duration: 2000
        });
      }
    }, {
        token: wx.getStorageSync('token')
      })
  },
  showRefundReject: function () {
    this.setData({
      showRefundReject: true
    })
  },
  closeRefundReject: function () {
    this.setData({
      showRefundReject: false
    })
  }
})
